package com.GlobalPaint.app.ui.Mine;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.util.LruCache;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.GlobalPaint.app.Base.BaseFragment;
import com.GlobalPaint.app.R;
import com.GlobalPaint.app.bean.EventBusben;
import com.GlobalPaint.app.bean.PersonalBean;
import com.GlobalPaint.app.bean.ReturnStatusResultEntity;
import com.GlobalPaint.app.ui.App;
import com.GlobalPaint.app.ui.Login.Login;
import com.GlobalPaint.app.ui.Mine.Laborer.MyJobWantedActivity;
import com.GlobalPaint.app.ui.Mine.Personal.PersonalActivity;
import com.GlobalPaint.app.ui.Mine.setting.SettingActivity;
import com.GlobalPaint.app.utils.Constants;
import com.GlobalPaint.app.utils.DataManager;
import com.blankj.utilcode.utils.SPUtils;
import com.jzy.message.util.xmpp.XmppUtil;
import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.NetworkPolicy;
import com.squareup.picasso.Picasso;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import static android.app.Activity.RESULT_OK;


public class MineFragment extends BaseFragment implements View.OnClickListener{
    private TextView bt_tuichu;
    private RelativeLayout rl_setting;
    private RelativeLayout rl_resume;
    private RelativeLayout rl_information;
    private RelativeLayout rl_attention;
    private RelativeLayout rl_fans;
    private RelativeLayout rl_share;
    private RelativeLayout rl_fabu;
    private ImageView qiandao;
    private PersonalBean personalBean;
    private ImageView touxiang;
    private TextView tv_nick;
    private TextView tv_yhm;
    private ImageView iv_goback;
    private TextView tv_common_center;
    private RelativeLayout rl_shoucang;
    private RelativeLayout rl_fankui;
    private RelativeLayout rl_msr;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
            View view=inflater.inflate(R.layout.fragment_mine, container, false);
        EventBus.getDefault().register(this);
            initView(view);
            getInformation();
            return view;
    }
    //修改信息成功后 返回参数
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data){
        switch (requestCode){
            case 1:{
                if(resultCode ==RESULT_OK){
                    tv_nick.setText(data.getStringExtra("ddd"));
                }
            }
        }
    }
    //点击头像上传成功返回头像
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void FirstEvent(EventBusben message) {
        String msg = message.getNr();

        if (msg == "touxiang") {
            getInformation();

        }
    }
    private void initView(View view) {
        iv_goback = (ImageView) view.findViewById(R.id.iv_goback);
        iv_goback.setVisibility(View.GONE);
        tv_common_center = (TextView)view.findViewById(R.id.tv_common_center);
        tv_common_center.setText("我的");
        bt_tuichu = (TextView)view. findViewById(R.id.bt_tuichu);
        bt_tuichu.setOnClickListener(this);
        rl_setting = (RelativeLayout) view.findViewById(R.id.rl_setting);
        rl_setting.setOnClickListener(this);
        rl_resume = (RelativeLayout) view.findViewById(R.id.rl_resume);
        rl_resume.setOnClickListener(this);
        rl_information = (RelativeLayout)view.findViewById(R.id.rl_information);
        rl_information.setOnClickListener(this);
        rl_attention = (RelativeLayout)view. findViewById(R.id.rl_attention);
        rl_attention.setOnClickListener(this);
        rl_fans = (RelativeLayout)view. findViewById(R.id.rl_fans);
        rl_fans.setOnClickListener(this);
        rl_share = (RelativeLayout)view.findViewById(R.id.rl_share);
        rl_share.setOnClickListener(this);
        rl_fabu = (RelativeLayout)view.findViewById(R.id.rl_fabu);
        rl_fabu.setOnClickListener(this);
        qiandao = (ImageView)view.findViewById(R.id.qiandao);
        qiandao.setOnClickListener(this);
        touxiang = (ImageView)view.findViewById(R.id.touxiang);
        tv_nick = (TextView)view. findViewById(R.id.tv_nick);
        tv_yhm = (TextView)view. findViewById(R.id.tv_yhm);
        rl_shoucang = (RelativeLayout)view. findViewById(R.id.rl_shoucang);
        rl_shoucang.setOnClickListener(this);
        rl_fankui = (RelativeLayout) view.findViewById(R.id.rl_fankui);
        rl_fankui.setOnClickListener(this);
        rl_msr = (RelativeLayout) view.findViewById(R.id.rl_msr);
        rl_msr.setOnClickListener(this);

    }
    private void getInformation() {
        String url= Constants.JASON_SERVICE_URL+"user/getUserById?userid="+ DataManager.userEntity.getUserId();
        DataManager.getInst().getHttpRequestJsonClass(url, DataManager.userEntity.getCookie(), PersonalBean.class, new DataManager.Callback() {
            @Override
            public void onSuccess(Object o) {
                personalBean=(PersonalBean)o;
                if (personalBean.getStatus() == 1){
                    if(personalBean.getData() != null ){
                        String Url= personalBean.getData().getAvatar()== null?"wu":
                                personalBean.getData().getAvatar();
                        String nick= personalBean.getData().getFNickName()== null?"":
                                personalBean.getData().getFNickName();
                        String mobile= personalBean.getData().getFMobile()== null?"":
                                personalBean.getData().getFMobile();
                        if(Url.equals("wu")){
                            Picasso.with(getContext()).load(R.drawable.touxiang).into(touxiang);
                        }else {

                            Picasso.with(App.context).load(Constants.JASON_SERVICE_URL_TOUXIANG+Url)
                                    .memoryPolicy(MemoryPolicy.NO_CACHE)

                                    .networkPolicy(NetworkPolicy.NO_CACHE)

                                    .placeholder(R.drawable.touxiang).into(touxiang);
                        }
                        if(nick!=null){
                            tv_nick.setText(nick);
                            tv_yhm.setText(DataManager.userEntity.getUserName());
                        }else {
                            tv_nick.setText(DataManager.userEntity.getUserName());
                            tv_yhm.setVisibility(View.GONE);
                        }
                        tv_nick.setText(nick);
                        tv_yhm.setText(DataManager.userEntity.getUserName());
                    }
                }
                else {
                    Toast.makeText(getContext(),personalBean.getMsg(),Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(ReturnStatusResultEntity error) {
            }
        });
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.qiandao:
                Intent Intent8=new Intent(getContext(), QiandaoActivity.class);
                startActivity(Intent8);
                break;
            case R.id.rl_attention:
                Intent Intent4=new Intent(getContext(), MyGuanzhuActivity.class);
                startActivity(Intent4);
                break;
            case R.id.rl_fans:
                Intent Intent5=new Intent(getContext(), MyFensiActivity.class);
                startActivity(Intent5);
                break;
            case R.id.rl_resume:
                Intent Intent3=new Intent(getContext(), ResumeActivity.class);
                Intent3.putExtra("activity","MineFragment");
                startActivity(Intent3);
                break;
            case R.id.rl_setting:
                Intent Intent2=new Intent(getContext(), SettingActivity.class);
                startActivity(Intent2);
                break;
            case R.id.bt_tuichu:
                SPUtils spUtils = new SPUtils(DataManager.context, "login");
                spUtils.remove("user");
                DataManager.userEntity.setDate("");
                DataManager.userEntity.getDate();
                EventBusben baseMessage = new EventBusben("tuichu");
                EventBus.getDefault().post(baseMessage);
                //退出的时候  openfeire也退出
                XmppUtil.LogOut();

                //退出时清除缓存
                DataManager.lruCache = new LruCache<>(10);
                Intent intent = new Intent(getActivity(), Login.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                getActivity().finish();
                break;
            case R.id.rl_information:
                Intent Intent1=new Intent(getContext(), PersonalActivity.class);
                Intent1.putExtra("main","mine");
                startActivityForResult(Intent1,1);
                break;
            case R.id.rl_share:
                Intent Intent6=new Intent(getContext(), ShareActivity.class);
                startActivity(Intent6);
                break;
            case R.id.rl_fabu:
                Intent Intent7=new Intent(getContext(), MyJobWantedActivity.class);
                startActivity(Intent7);
                break;
            case R.id.rl_shoucang:
                Intent Intent10=new Intent(getContext(), MyShouCActivity.class);
                startActivity(Intent10);
                break;
            case R.id.rl_fankui:
                Intent Intent9=new Intent(getContext(), FankuiActivity.class);
                startActivity(Intent9);
                break;
            case R.id.rl_msr:
                Intent Intent11=new Intent(getContext(), MsrActivity.class);
                startActivity(Intent11);
                break;

        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
